package com.h3bpm.web.controller;

import OThinker.H3.Controller.ControllerBase;
import OThinker.H3.Controller.Controllers.Organization.CodeInfoController;
import org.jboss.netty.handler.codec.http.HttpHeaders;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.*;
import java.util.List;

@Controller
@RequestMapping({"/Portal/IE/Validate"})
public class IECompatibleController extends ControllerBase {

    private static final Logger logger = LoggerFactory.getLogger(IECompatibleController.class);
    private int width = 90;
    private int height = 40;
    private int codeCount = 4;
    private int lineCount = 19;
    char[] codeSequence = new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

    @RequestMapping(
            value = {"/createCode"},
            method = {RequestMethod.GET}
    )
    @ResponseBody
    public void CreateCode(HttpServletRequest request, HttpServletResponse response, String userCode){
        Random r = new Random();
        StringBuilder builderCode = new StringBuilder();
        BufferedImage buffImg = new BufferedImage(this.width, this.height, 1);
        Graphics g = buffImg.getGraphics();
        g.setColor(Color.black);
        g.drawRect(0, 0, this.width, this.height);
        g.setColor(Color.white);
        g.fillRect(1, 1, this.width - 2, this.height - 2);
        g.setColor(Color.gray);

        int i;
        for(i = 0; i < this.lineCount; ++i) {
            g.drawLine(r.nextInt(this.width), r.nextInt(this.width), r.nextInt(this.width), r.nextInt(this.width));
        }

        g.setColor(Color.blue);
        g.setFont(new Font("宋体", 3, 15));

        for(i = 0; i < this.codeCount; ++i) {
            char c = this.codeSequence[r.nextInt(this.codeSequence.length)];
            builderCode.append(c);
            g.drawString(c + "", 15 * (i + 1), 25);
        }

        ServletOutputStream sos = null;

        try {
            response.setHeader("Content-Type" ,"image/png");
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0L);
            sos = response.getOutputStream();
            HttpSession session = request.getSession();
            session.setAttribute("codeValidate", builderCode.toString());
            ImageIO.write(buffImg, "png", sos);
        } catch (IOException var11) {
            var11.printStackTrace();
        }



        try {
            sos.close();
        } catch (IOException var10) {
            var10.printStackTrace();
        }

    }

    @Override
    public String getFunctionCode() {
        return null;
    }
}
